Crate stream_throttle
source ·Expand description
Provides a
Stream
combinator, to limit the rate at which
items are produced.
Key Features
- Throttling is implemented via
poll()
, and not via any sort of buffering. - The throttling behaviour can be applied to both
Stream
’s andFuture
’s. - Multiple streams/futures can be throttled together as a group.
- Feature flags to use various timer implementations.
Feature Flags
timer-tokio
: Uses thetokio::time::delay_for()
timer (default).timer-futures-timer
: Uses thefutures_timer::Delay
timer.
If you don’t use the default timer (tokio
), make sure to set default-features = false
in your Cargo.toml
, when you add stream_throttle
as a dependency.
Example throttling of Stream
// allow no more than 5 items every 1 second
let rate = ThrottleRate::new(5, Duration::new(1, 0));
let pool = ThrottlePool::new(rate);
let work = futures::stream::repeat(())
.throttle(pool)
.then(|_| futures::future::ready("do something else"))
.for_each(|_| futures::future::ready(()));
futures::executor::block_on(work);
Example throttling of Future
let rate = ThrottleRate::new(5, Duration::new(1, 0));
let pool = ThrottlePool::new(rate);
let work = pool.queue()
.then(|_| futures::future::ready("do something else"));
futures::executor::block_on(work);
Structs
- A clonable object which is used to throttle one or more streams, according to a shared rate.
- Defines the the throttle rate.
- A stream combinator which throttles its elements via a shared
ThrottlePool
.
Traits
- Provides a
throttle()
method on allStream
’s.